<?php
/**
**畅游通对接
**2015-05-27 
**User by pzp
**/
/* class ChangYouTong{
	public function __construct($UUlid){
		//$this->client = new SoapClient('http://www.jingqu.cn/services/ectripOTAService?wsdl'); //畅游通
			$this->client = new SoapClient('http://119.147.209.55/services/ectripOTAService?wsdl'); //畅游通
		switch ($UUlid){
			case 8510:  //百合-野生动物园
				$this->createUser = 'cytbaihe';
				$this->supplierIdentity = 'CYTSPQHDZOOB2C';
				$this->signkey = 'ae15qa77ed61a72ed';
			break;	
			case 8529:  //海源旅行社-野生动物园
				$this->createUser = 'cytheyuan';
				$this->supplierIdentity = 'CYTSPQHDZOOB2C';
				$this->signkey = 'cd50ac7r2ce9071c2';
			break;
			case 8997:  //海源旅行社-碧螺塔
				$this->createUser = 'cytheyuan';
				$this->supplierIdentity = 'CYTSPBILUOTAB2C';
				$this->signkey = '9t8a6c12fa7yu52f3';
			break;
			case 9412:  //百合旅行社-碧螺塔
				$this->createUser = 'cytbaihe';
				$this->supplierIdentity = 'CYTSPBILUOTAB2C';
				$this->signkey = 's41c4def9ff25c62d';
			break;			
			case 9389:  //海源-集发
				$this->createUser = 'cytheyuan';
				$this->supplierIdentity = 'CYTSPJFGGY';
				$this->signkey = '9bcx8e9v6ca52beh9';
			break;			
			case 10249:  //百合-集发
				$this->createUser = 'cytbaihe';
				$this->supplierIdentity = 'CYTSPJFGGY';
				$this->signkey = '7wue4f2c74e5i8ed3';
			break;
			case 9290:  //畅游通的测试景点
				$this->createUser = 'cyttongcheng';
				$this->supplierIdentity = 'CYTSPWDSB2C';
				$this->signkey = 'qwer';
				$this->client = new SoapClient('http://demo.jingqu.cn/services/ectripOTAService?wsdl'); //畅游通
			break;
		}
	}
	
	public function getProductByOTA(){
		$time = date('Y-m-d H:i:s');
		$request_xml =<<<XML
<?xml version="1.0" encoding="UTF-8"?>
<request xmlns:qm="http://piao.ectrip.com/2014/QMenpiaoRequestSchema"
	xsi:schemaLocation="http://piao.ectrip.com/2014/QMenpiaoRequestSchema QMRequestDataSchema-1.1.0.xsd"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<header>
		<application>tour.ectrip.com</application>
		<processor>DataExchangeProcessor</processor>
		<version>v2.0.0</version>
		<bodyType>GetProductByOTARequestBody</bodyType>
		<createUser>{$this->createUser}</createUser>
		<createTime>{$time}</createTime>
		<supplierIdentity>{$this->supplierIdentity}</supplierIdentity>
	</header>
	<body xsi:type="GetProductByOTARequestBody">
		<method>ALL</method>
		<currentPage>1</currentPage>
		<pageSize>10</pageSize>
		<resourceId>2</resourceId>
	</body>
</request>
XML;
		$cyt_PostData = $this->signed($request_xml,$this->signkey);
		$cyt_Response = $this->client->doOTARequest("getProductByOTA",$cyt_PostData);
		$cyt_Response_xml = $this->turn_xml($cyt_Response);
		print_r($cyt_Response_xml);exit;
		$cyt_Response_xml = simplexml_load_string(base64_decode($cyt_Response_data['data'])); 
		print_r($cyt_Response_xml);
	}
	
	//创建订单
	public function createPaymentOrder($request_array){		
		$price = $request_array['UUlprice'];
		$orderallPrice = $request_array['UUtnum']* $request_array['UUlprice'];
		$request_xml=<<<XML
<?xml version="1.0" encoding="UTF-8"?>
<request xmlns:qm="http://piao.ectrip.com/2014/QMenpiaoRequestSchema"
	xsi:schemaLocation="http://piao.ectrip.com/2014/QMenpiaoRequestSchema QMRequestDataSchema-1.1.0.xsd"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<header>
		<application>tour.ectrip.com</application>
		<processor>DataExchangeProcessor</processor>
		<version>v2.0.0</version>
		<bodyType>CreateOrderPaymentRequestBody</bodyType>
		<createUser>{$this->createUser}</createUser>
		<createTime>{$request_array['UUordertime']}</createTime>
		<supplierIdentity>{$this->supplierIdentity}</supplierIdentity>
	</header>
	<body xsi:type="CreatePaymentOrderRequestBody">
		<orderInfo>
			<orderId>{$request_array['pftordernum']}</orderId>
			<product>
				<resourceId>{$request_array['uuid']}</resourceId>
				<productName>{$request_array['productname']}</productName>
				<visitDate>{$request_array['UUplaytime']}</visitDate>
				<sellPrice>{$price}</sellPrice>
			</product>
			<contactPerson>
				<name>{$request_array['pftordername']}</name>
				<mobile>{$request_array['UUordertel']}</mobile>
				<credentials>{$request_array['IDcar']}</credentials>
				<credentialsType>ID_CARD</credentialsType>
			</contactPerson>
			<visitPerson>
				<person>
					<name>{$request_array['UUordertel']}</name>
					<credentials>{$request_array['IDcar']}</credentials>
					<credentialsType>ID_CARD</credentialsType>
				</person>
			</visitPerson>
			<orderQuantity>{$request_array['UUtnum']}</orderQuantity>
			<orderPrice>{$orderallPrice}</orderPrice>
			<orderStatus>PREPAY_ORDER_PRINTING</orderStatus>
		</orderInfo>
	</body>
</request>
XML;
		// print_r($request_xml);
		$cyt_PostData = $this->signed($request_xml,$this->signkey);
		$cyt_Response = $this->client->doOTARequest("createPaymentOrder",$cyt_PostData);		
		$cyt_Response_xml = $this->turn_xml($cyt_Response);
		$cyt_Response_xml_code = (int)$cyt_Response_xml->header->code;
		$cyt_Response_xml_describe = (string)$cyt_Response_xml->header->describe;
		// print_r($cyt_Response_xml);exit;
		if($cyt_Response_xml_code == 1000 ){ //1000是成功推送的 
			$this->logfile($request_array['pftordernum']."|".(string)$cyt_Response_xml->body->orderInfo->partnerorderId,'send_success');
			return $cyt_Response_xml->body->orderInfo->partnerorderId;
		}else{
			$this->logfile($request_array['pftordernum']."|".$cyt_Response_xml_code."|".$cyt_Response_xml_describe,'send_fail');
			return 101;
		}
	
	}
	
	//修改及取消订单
	public function applyOrderRefundByUser($request_array){
		$request_xml=<<<XML
<?xml version="1.0" encoding="UTF-8"?>
<request xmlns:qm="http://piao.ectrip.com/2014/QMenpiaoRequestSchema"
	xsi:schemaLocation="http://piao.ectrip.com/2014/QMenpiaoRequestSchema QMRequestDataSchema-1.1.0.xsd"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<header>
		<application>tour.ectrip.com</application>
		<processor>DataExchangeProcessor</processor>
		<version>v2.0.0</version>
		<bodyType>ApplyOrderRefundByUserRequestBody</bodyType>
		<createUser>{$this->createUser}</createUser>
		<createTime>{$request_array['rectime']}</createTime>
		<supplierIdentity>{$this->supplierIdentity}</supplierIdentity>
	</header>
	<body xsi:type="ApplyOrderRefundByUserRequestBody">
		<orderInfo>
			<partnerorderId>{$request_array['APIOrdern']}</partnerorderId>
			<refundSeq>{$request_array['refundSeq']}</refundSeq>
			<orderPrice>{$request_array['orderPrice']}</orderPrice>
			<orderQuantity>{$request_array['orderQuantity']}</orderQuantity>
			<refundQuantity>{$request_array['refundQuantity']}</refundQuantity>
			<orderRefundPrice>{$request_array['orderRefundPrice']}</orderRefundPrice>
			<orderRefundCharge>{$request_array['orderRefundCharge']}</orderRefundCharge>
			<refundExplain>{$request_array['refundExplain']}</refundExplain>
			<visitPerson>
				<person>
					<name>{$request_array['name']}</name>
					<credentials>{$request_array['idcard']}</credentials>
					<credentialsType>ID_CARD</credentialsType>
				</person>
			</visitPerson>
		</orderInfo>
	</body>
</request>
XML;
		$cyt_PostData = $this->signed($request_xml,$this->signkey);
		$cyt_Response = $this->client->doOTARequest("applyOrderRefundByUser",$cyt_PostData);
		$cyt_Response_xml = $this->turn_xml($cyt_Response);
		$cyt_Response_xml_code = (int)$cyt_Response_xml->header->code;
		$cyt_Response_xml_describe = (string)$cyt_Response_xml->header->describe;
		if($cyt_Response_xml_code == 1000 ){  //1000是申请修改成功 
			$this->logfile($request_array['APIOrdern']."|".$cyt_Response_xml_code."|".$cyt_Response_xml_describe,"modify_success");
			return 100;
		}else{
			$this->logfile($request_array['APIOrdern']."|".$cyt_Response_xml_code."|".$cyt_Response_xml_describe,"modify_fail");
			return 101;
		}
	}
	
	//查看订单
	public function getOrderByOTA($cyt_ordernum){
		$time = date('Y-m-d H:i:s');
		$request_xml=<<<XML
<?xml version="1.0" encoding="UTF-8"?>
<request xmlns:qm="http://piao.ectrip.com/2014/QMenpiaoRequestSchema"
	xsi:schemaLocation="http://piao.ectrip.com/2014/QMenpiaoRequestSchema QMRequestDataSchema-1.1.0.xsd"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<header>
		<application>tour.ectrip.com</application>
		<processor>DataExchangeProcessor</processor>
		<version>v2.0.0</version>
		<bodyType>GetOrderByOTARequestBody</bodyType>
		<createUser>{$this->createUser}</createUser>
		<createTime>{$time}</createTime>
		<supplierIdentity>{$this->supplierIdentity}</supplierIdentity>
	</header>
	<body xsi:type="GetOrderByOTARequestBody">
		<partnerOrderId>{$cyt_ordernum}</partnerOrderId>
	</body>
</request>
XML;
		$cyt_PostData = $this->signed($request_xml,$this->signkey);
		$cyt_Response = $this->client->doOTARequest("getOrderByOTA",$cyt_PostData);		
		$cyt_Response_xml = $this->turn_xml($cyt_Response);
		// print_r($cyt_Response_xml);
	}
	
	public function signed($request_xml,$signkey){ //这里是加密
		$bstr=base64_encode($request_xml);
		$signed=strtoupper(md5($signkey.$bstr));
		$arr=array('data'=>$bstr,'signed'=>$signed,'securityType'=>'MD5');
		
		return json_encode($arr);
	}
	
	public function turn_xml($cyt_Response){		
		$cyt_Response_data = json_decode($cyt_Response,true);
		$cyt_Response_data = str_replace("qm:","",$cyt_Response_data);
		$cyt_Response_xml = base64_decode($cyt_Response_data['data']);
		$cyt_Response_xml = str_replace("qm:","",$cyt_Response_xml);
		$cyt_Response_xml = simplexml_load_string($cyt_Response_xml);
		// print_r($cyt_Response_xml);
		return $cyt_Response_xml;		
	}	
		
	public function logfile($txt,$logname){
		$fp = fopen('/var/www/html/new/d/logs/api/ChangYouTong/'.$logname.date('Y-m-d').'.txt',"a");
		flock($fp, LOCK_EX) ;
		fwrite($fp,date('Y-m-d H:i:s').'|'.$txt."\n");
		flock($fp, LOCK_UN);
		fclose($fp); 
	}	
}
 */
// $cyt = new ChangYouTong(9290);
// $array = Array(
    // "pftordernum" => 1216043,
    // "pftordername" => "测试pzp",
    // "UUtnum" => 1,
    // "UUordertime" => "2015-05-28 16:29:30",
    // "UUplaytime" => "2015-06-13",
    // "UUendtime" => "2015-06-15",
    // "UUtprice" => 70,
    // "UUlprice" => 100,
    // "UUordertel" => "15959110796",
    // "uuid" => "2",
    // "IDcar" => "350321199108230123",
    // "productname" => "武当山门票+车票+保险-游客",
    // "UUltitle" => "武当山风景区测试",
    // "UUcode" => 522459,
    // "ispay" => 1,
// );

// $submit_order = $cyt->createPaymentOrder($array);
// $getProductByOTA = $cyt->getProductByOTA();

// $getOrderByOTA = $cyt->getOrderByOTA('20150606999000084');
/*
$request_xml1=<<<XML
<?xml version="1.0" encoding="UTF-8"?>
<request xmlns:qm="http://piao.ectrip.com/2014/QMenpiaoRequestSchema"
	xsi:schemaLocation="http://piao.ectrip.com/2014/QMenpiaoRequestSchema QMRequestDataSchema-1.1.0.xsd"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<header>
		<application>tour.ectrip.com</application>
		<processor>DataExchangeProcessor</processor>
		<version>v2.0.0</version>
		<bodyType>CreateOrderPaymentRequestBody</bodyType>
		<createUser>OTA用户标识</createUser>
		<createTime>2014-04-23 11:29:37</createTime>
		<supplierIdentity>供应商标识</supplierIdentity>
	</header>
	<body xsi:type="CreatePaymentOrderRequestBody">
		<orderInfo>
			<orderId>123456</orderId>
			<product>
				<resourceId></resourceId>
				<productName></productName>
				<visitDate></visitDate>
				<sellPrice></sellPrice>
			</product>
			<contactPerson>
				<name></name>
				<mobile></mobile>
				<credentials></credentials>
				<credentialsType></credentialsType>
			</contactPerson>
			<visitPerson>
				<person>
					<name></name>
					<credentials></credentials>
					<credentialsType></credentialsType>
				</person>
				<person>
					<name></name>
					<credentials></credentials>
					<credentialsType></credentialsType>
				</person>
				<person>
					<name></name>
					<credentials></credentials>
					<credentialsType></credentialsType>
				</person>
			</visitPerson>
			<orderQuantity></orderQuantity>
			<orderPrice></orderPrice>
			<orderStatus></orderStatus>
		</orderInfo>
	</body>
</request>
XML;
$request_xml2 =  simplexml_load_string($request_xml1);
print_r((string)$request_xml2->body->orderInfo->orderId);
*/
?>